home *** CD-ROM | disk | FTP | other *** search
- #include "EnetVersion.h"
- #include <stropts.h>
- #include <dlpi.h>
- #include <poll.h>
- #include <DriverServices.h>
- #define MACOS 1
- #include "EnetUser.h"
-
- #include "Promiscuity.h"
- #include "SortFrames.h"
-
- #define printf(...)
-
- #define UseEndPoint 1
-
- /******************************************************************************/
-
- // dl_enabpromisc_req() does a DL_PROMISCON_REQ.
- // The stream will receive all packets.
- // Returns false if there was an error.
- static Boolean dl_enabpromisc_req(int stream)
- {
- struct strbuf control;
- unsigned char enabpromiscBuffer[sizeof(dl_promiscon_req_t)];
- dl_promiscon_req_t * enabpromiscReq =
- (dl_promiscon_req_t *)enabpromiscBuffer;
- unsigned char okAckBuffer[sizeof(dl_ok_ack_t)];
- dl_ok_ack_t * okAck = (dl_ok_ack_t *)okAckBuffer;
- int flags;
- int err;
-
- // Prepare a DL_PROMISCON_REQ message.
- enabpromiscReq->dl_primitive = DL_PROMISCON_REQ;
- enabpromiscReq->dl_level = DL_PROMISC_PHYS;
-
- // Send the DL_PROMISCON_REQ message downstream to the DLPI module.
- control.len = sizeof(enabpromiscBuffer);
- control.buf = (char *)enabpromiscBuffer;
- if (putmsg(stream, &control, NULL, 0) < 0) {
- printf("dl_enabpromisc_req: putmsg errno %d\n", errno);
- goto error;
- }
-
- // Get the DL_OK_ACK message sent upstream by the DLPI module.
- control.maxlen = sizeof(okAckBuffer);
- control.len = 0;
- control.buf = (char *)okAckBuffer;
- flags = RS_HIPRI;
- if (err = getmsg(stream, &control, NULL, &flags) < 0) {
- printf("dl_enabpromisc_req: getmsg errno %d\n", err);
- goto error;
- }
- if (control.len < sizeof(UInt32)) {
- printf("dl_enabpromisc_req: control.len %d < %d\n",
- control.len,
- sizeof(UInt32));
- goto error;
- }
- if (okAck->dl_primitive == DL_ERROR_ACK) {
- printf("dl_enabpromisc_req: DL_ERROR_ACK\n");
- goto error;
- }
- if (okAck->dl_primitive != DL_OK_ACK) {
- printf("dl_enabpromisc_req: 0x%08X not DL_OK_ACK\n",
- okAck->dl_primitive);
- goto error;
- }
-
- // All done, no error, return true.
- return true;
-
- // Return false if there was an error.
- error:
- return false;
-
- }
-
- /******************************************************************************/
-
- // dl_disabpromisc_req() does a DL_PROMISCOFF_REQ.
- // The stream will receive all packets.
- // Returns false if there was an error.
- static Boolean dl_disabpromisc_req(int stream)
- {
- struct strbuf control;
- unsigned char enabpromiscBuffer[sizeof(dl_promiscon_req_t)];
- dl_promiscon_req_t * enabpromiscReq =
- (dl_promiscon_req_t *)enabpromiscBuffer;
- unsigned char okAckBuffer[sizeof(dl_ok_ack_t)];
- dl_ok_ack_t * okAck = (dl_ok_ack_t *)okAckBuffer;
- int flags;
- int err;
-
- // Prepare a DL_PROMISCOFF_REQ message.
- enabpromiscReq->dl_primitive = DL_PROMISCOFF_REQ;
- enabpromiscReq->dl_level = DL_PROMISC_PHYS;
-
- // Send the DL_PROMISCON_REQ message downstream to the DLPI module.
- control.len = sizeof(enabpromiscBuffer);
- control.buf = (char *)enabpromiscBuffer;
- if (putmsg(stream, &control, NULL, 0) < 0) {
- printf("dl_disabpromisc_req: putmsg errno %d\n", errno);
- goto error;
- }
-
- // Get the DL_OK_ACK message sent upstream by the DLPI module.
- control.maxlen = sizeof(okAckBuffer);
- control.len = 0;
- control.buf = (char *)okAckBuffer;
- flags = RS_HIPRI;
- if (err = getmsg(stream, &control, NULL, &flags) < 0) {
- printf("dl_disabpromisc_req: getmsg errno %d\n", err);
- goto error;
- }
- if (control.len < sizeof(UInt32)) {
- printf("dl_disabpromisc_req: control.len %d < %d\n",
- control.len,
- sizeof(UInt32));
- goto error;
- }
- if (okAck->dl_primitive == DL_ERROR_ACK) {
- printf("dl_disabpromisc_req: DL_ERROR_ACK\n");
- goto error;
- }
- if (okAck->dl_primitive != DL_OK_ACK) {
- printf("dl_enabpromisc_req: 0x%08X not DL_OK_ACK\n",
- okAck->dl_primitive);
- goto error;
- }
-
- // All done, no error, return true.
- return true;
-
- // Return false if there was an error.
- error:
- return false;
-
- }
-
- /******************************************************************************/
-
- // dl_unitdata_ind() handles a DL_UNITDATA_IND.
- // This is the basic receive operation.
- // This function blocks until a packet has been received.
- // Returns false if there was an error.
- static Boolean
- dl_unitdata_ind(int stream,
- UInt32 maxPacketSize,
- char *packet,
- UInt32 *packetSize,
- EnetAddressPtr address,
- Boolean block)
- {
- struct pollfd pfd[1];
- int n;
- struct strbuf control;
- struct strbuf data;
- int flags;
- unsigned char unitdataIndBuffer[sizeof(dl_unitdata_ind_t) + 26];
- dl_unitdata_ind_t * unitdataInd = (dl_unitdata_ind_t *)unitdataIndBuffer;
- static int taskCount;
-
- for (;;) {
- // Poll for an input message ready.
- pfd[0].fd = stream;
- pfd[0].events = POLLIN;
- if (((n = poll(pfd, 1, block ? 1 : 0)) < 0) || (n > 1)) {
- printf("WDiskServer: poll error, n = %d\n", n);
- break;
- }
- // If timeout, do SystemTask() and check for Quit.
- if (n == 0) {
- if (block || ((taskCount++ & 1023) == 0)) {
- //SystemTask();
- }
- if (!block)
- goto error;
- continue;
- }
- // A message is ready. Apply getmsg().
- control.maxlen = sizeof(unitdataIndBuffer);
- control.len = 0;
- control.buf = (char *)unitdataIndBuffer;
- data.maxlen = maxPacketSize;
- data.len = 0;
- data.buf = packet;
- flags = 0;
- if ((n = getmsg(stream, &control, &data, &flags)) != 0) {
- printf("WDiskServer: getmsg error, n = %d\n", n);
- continue;
- }
- // If control length is -1, then it is a FAST-PATH message.
- // Note that a FAST-PATH message does not have the source address.
- if (control.len == -1)
- ;
- // Error if the control length is out of bounds.
- else if (control.len < sizeof(UInt32)) {
- printf("WDiskServer: control.len %d < %d\n",
- control.len,
- sizeof(UInt32));
- continue;
- }
- // Error if the message is not DL_UNITDATA_IND.
- else if (unitdataInd->dl_primitive != DL_UNITDATA_IND) {
- // printf("WDiskServer: dl_primitive %d != %d\n",
- // unitdataInd->dl_primitive,
- // DL_UNITDATA_IND);
- continue;
- }
- // Process the DL_UNITDATA_IND message.
- // Note that a DL_UNITDATA_IND message has the source address.
- else {
- BlockMove(&unitdataIndBuffer[unitdataInd->dl_src_addr_offset],
- address,
- 6);
- }
- // Export the packet size.
- *packetSize = data.len;
- break;
- }
-
- // All done, no error, return true.
- return true;
-
- // Return false if there was an error.
- error:
- return false;
-
- }
-
- /******************************************************************************/
-
- //===
-
- static int stream = -1;
-
- void initPromiscuity(void)
- {
- stream = stream_open("enet1", 0);
- dl_enabpromisc_req(stream);
- }
-
- void idlePromiscuity(void)
- {
- char rxP[1500];
- Packet *p = (Packet*) rxP;
- UInt32 size;
- UInt8 address[6] = {0,};
-
- if (dl_unitdata_ind(stream, 1500, rxP, &size, address, true))
- {
- if ((p->protocol == 6) && ((p->versionAndIHL & 0x0F) == 5))
- {
- if ((p->totalLength > 40) || (p->moreFlagsAndJunk & kFINBit))
- {
- ConsumePacket( p );
- }
- else showBlob( 0 ); // yellow
- }
- else showBlob( 0 ); // yellow
- }
- }
-
- void termPromiscuity(void)
- {
- dl_disabpromisc_req(stream);
- }
-
-
-